package yl.hs.bmipfull.service.finance.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.finance.IShipInvoiceRecordDao;
import yl.hs.bmipfull.daos.finance.IShipSettleMainDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.pojo.finance.ShipInvoiceRecordEntity;
import yl.hs.bmipfull.pojo.finance.ShipSettleMainEntity;
import yl.hs.bmipfull.service.finance.IShipInvoiceRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 开票记录-ServiceImpl
 * @since 2022/1/11 4:05 下午 创建
 */
@Service
public class ShipInvoiceRecordServiceImpl extends FinanceService implements IShipInvoiceRecordService {

    @Autowired
    IShipInvoiceRecordDao invoiceRecordDao;
    @Autowired
    IShipSettleMainDao shipSettleMainDao;

    @Autowired
    IProductShipOrderDao productShipOrderDao;

    @Override
    public BigDecimal sumInvoiceAmountByContractSn(String contractSn) {
        BigDecimal amount = invoiceRecordDao.sumInvoiceAmountByContractSn(contractSn);
        return amount == null?BigDecimal.ZERO:amount;
    }


    @Override
    public List<ShipInvoiceRecordEntity> queryList(PageQuery pageQuery)throws Exception{
        pageQuery.checkSortSides(ShipInvoiceRecordEntity.class);
        return invoiceRecordDao.queryPage(pageQuery.getQuery());
    }
    @Override
    public PageResult<ShipInvoiceRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page)throws Exception{
        pageQuery.checkSortSides(ShipInvoiceRecordEntity.class);
        pageQuery.getQuery().put("querySkip",(page-1)*rowNum);
        pageQuery.getQuery().put("pageSize",rowNum);
        PageResult<ShipInvoiceRecordEntity> result=new PageResult(rowNum);
        result.setRecords(invoiceRecordDao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(invoiceRecordDao.queryPage(pageQuery.getQuery()));
        return result;
    }



    @Override
    public ShipInvoiceRecordEntity findBySn(String sn){
        return invoiceRecordDao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveShipInvoiceRecord(Map<String, Object> model, String sn)throws Exception{
        Integer result=0;
        ShipInvoiceRecordEntity entity=null;
        var shipSettleSN="";
        if(model.get("shipSettleSN")!=null){
            shipSettleSN=model.get("shipSettleSN").toString();
        }
        if(Tl.isEmpty(sn)){
            //执行保存
            entity=new ShipInvoiceRecordEntity();
            Tl.getChangeLog(entity,model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ShipInvoiceRecordEntity.class));
            result=invoiceRecordDao.insertNew(entity);
            if(result>0){
                writeLog(ActionType.CREATE,entity.getSn(),"插入开票记录信息",Tl.toJson(entity), ShipInvoiceRecordEntity.class);
            }

        }else{
            //执行修改
            entity=findBySn(sn);
            Tl.getChangeLog(entity,model);
            entity.setPYFieldValue();
            result=invoiceRecordDao.update(entity);
            if(result>0){
                writeLog(ActionType.UPDATE,sn,"更新开票记录信息",Tl.toJson(entity), ShipInvoiceRecordEntity.class);
            }
        }
        if(shipSettleSN!=null && !shipSettleSN.equals("")){
            var shipSettle=shipSettleMainDao.findBySN(shipSettleSN);
            shipSettle.setState((short) 2);
            var c=shipSettleMainDao.update(shipSettle);
            if(c>0){
                writeLog(ActionType.UPDATE,sn,"更新为已开票状态",Tl.toJson(shipSettle), ShipSettleMainEntity.class);
            }
            var shipList=shipSettleMainDao.querySettleOrdersBySettleSN(shipSettleSN);
            if(shipList.size()>0){
                var snList=shipList.stream().map(t-> t.getSn()).toList();
                productShipOrderDao.updateInvoicingStateBySNList(1,snList);
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[]sn){
        Integer result=invoiceRecordDao.changeStateBySN(sn, DataState.DELETED.code);
        if(result>0){
            writeLog(ActionType.DELETE, StringUtils.join(sn,","),"删除开票记录信息",Tl.toJson(sn), ShipInvoiceRecordEntity.class);
        }
        return result;
    }
}
